from django import template
from django.template import Node

import logging

from bl_rb.models import Blurb

register = template.Library()

class BlurbNode(Node):
    def __init__(self, slug):
        self.slug = slug

    def render(self, context):
        try: 
            blurb = Blurb.objects.get(slug=self.slug)
        except Blurb.DoesNotExist:
            logging.warning("blurb not found %s" % self.slug)
            return ""
        return blurb.body

def blurb(parser, token):
    """
    Returns a blurb.
     
    """
    try:
        # split_contents() knows not to split quoted strings.
        tag_name, slug = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
    if not (slug[0] == slug[-1] and slug[0] in ('"', "'")):
        raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
    return BlurbNode(slug[1:-1])
    
blurb = register.tag(blurb)
